package com.hq.fxh.No1_Infromation.Video.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.hq.fxh.CustomerView.ShareDialogUtils;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.GetEntertainmentListResInfo;
import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No0_ChengQu.Activity.CommentDetailsActivity;
import com.hq.fxh.No1_Infromation.Video.Adapter.Adapter_List_VideoDetail;
import com.hq.fxh.No1_Infromation.Video.Adapter.Help_VideoDetils_Content;
import com.hq.fxh.No1_Infromation.Video.Adapter.Help_VideoDetils_TuiJian;
import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.ClickUtils;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;
import com.hq.fxh.Utils.Tool;

import java.io.IOException;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class VideoDetailsActivity extends BaseActivity implements View.OnClickListener {

    protected LinearLayout detailBack;
    protected LinearLayout detailMore;
    protected ListView recycleVideodetail;
    protected TextView imageNewsDetailsWrite;
    protected LinearLayout imageNewsDetailsComment;
    protected LinearLayout imageNewsDetailsShouChang;
    protected LinearLayout imageNewsDetailsShare;
    protected ImageView authorIcon;
    protected TextView authorName;
    protected JCVideoPlayerStandard videoplayer;
    protected ImageView imageShouChang;
    protected boolean isShouChang = true;
    protected boolean isZan = false;
    protected BridgeWebView webViewVideoDetails;

    GetEntertainmentListResInfo data = null;
    String videoPath = "/storage/emulated/0/DCIM/movie/RIDEO_haiqu43877.mp4";
    private String videoId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_video_details;
    }

    @Override
    protected void initView() {
        detailBack = (LinearLayout) findViewById(R.id.videoDetail_Back);
        detailBack.setOnClickListener(VideoDetailsActivity.this);
        detailMore = (LinearLayout) findViewById(R.id.videoDetail_More);
        detailMore.setOnClickListener(VideoDetailsActivity.this);
        //  recycleVideodetail = (RecyclerView) findViewById(R.id.recycle_videodetail);
        recycleVideodetail = (ListView) findViewById(R.id.recycle_videodetail);

        imageNewsDetailsWrite = (TextView) findViewById(R.id.image_NewsDetails_Write);
        imageNewsDetailsWrite.setOnClickListener(VideoDetailsActivity.this);
        imageNewsDetailsComment = (LinearLayout) findViewById(R.id.image_NewsDetails_Comment);
        imageNewsDetailsComment.setOnClickListener(VideoDetailsActivity.this);
        imageNewsDetailsShouChang = (LinearLayout) findViewById(R.id.image_NewsDetails_ShouChang);
        imageNewsDetailsShouChang.setOnClickListener(VideoDetailsActivity.this);
        imageNewsDetailsShare = (LinearLayout) findViewById(R.id.image_NewsDetails_Share);
        imageNewsDetailsShare.setOnClickListener(VideoDetailsActivity.this);
        authorIcon = (ImageView) findViewById(R.id.author_icon);
        authorName = (TextView) findViewById(R.id.author_name);
        videoplayer = (JCVideoPlayerStandard) findViewById(R.id.videoplayer);
        imageShouChang = (ImageView) findViewById(R.id.image_ShouChang);
        webViewVideoDetails = (BridgeWebView) findViewById(R.id.webView_VideoDetails);

    }

    @Override
    protected void initData() {

        Bundle bundle = getIntent().getBundleExtra("bundle");
        if (bundle != null) {
            data = (GetEntertainmentListResInfo) bundle.getSerializable("data");
        }

          videoId = data.getSubject_id();

        videoplayer.backButton.setVisibility(View.GONE);
        videoplayer.titleTextView.setVisibility(View.GONE);
        videoplayer.setUp(data == null ? videoPath : data.getSubject_ext(), JCVideoPlayer.SCREEN_LAYOUT_NORMAL, "");
        videoplayer.startPlayLogic();
        Help_VideoDetils_Content help_videoDetils_content = new Help_VideoDetils_Content(VideoDetailsActivity.this, null);
        recycleVideodetail.addHeaderView(help_videoDetils_content.getView());
        Help_VideoDetils_TuiJian help_videoDetils_tuiJian = new Help_VideoDetils_TuiJian(VideoDetailsActivity.this, null);
        recycleVideodetail.addHeaderView(help_videoDetils_tuiJian.getView());

        Adapter_List_VideoDetail adapter = new Adapter_List_VideoDetail(true, new Adapter_List_VideoDetail.CommentCallBack() {
            @Override
            public void call(int positon) {
                startActivity(new Intent(VideoDetailsActivity.this, CommentDetailsActivity.class));
            }

            @Override
            public void callZhan(int positon, TextView textView, ImageView imageView) {
                isZan = ClickUtils.clickZhan(VideoDetailsActivity.this, imageView, textView, isZan);
            }


            @Override
            public void callHuiFu(int positon) {

            }
        });
        recycleVideodetail.setAdapter(adapter);
        initWeb();
    }

    private void initWeb() {

        webViewVideoDetails.setWebChromeClient(new WebChromeClient());
        WebSettings settings = webViewVideoDetails.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptEnabled(true);//支持javascript
        settings.setUseWideViewPort(true);//适配屏幕
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);//支持放大缩小
        settings.setDisplayZoomControls(false);//隐藏放大缩小的按钮
        settings.setDomStorageEnabled(true);//支持Html5标签
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        int memberId = SharedPreferenceUtils.getMemberId(VideoDetailsActivity.this);
        String videoUrl = Tool.getVideoUrl(memberId,videoId,3);
        LogUtils.e("视频网站"+videoUrl);
        webViewVideoDetails.loadUrl(videoUrl);
        webViewVideoDetails.registerHandler("Appinteractive", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                LogUtils.e("指定Handler接收来自web的数据：" + data);

            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.videoDetail_Back) {
            finish();
        } else if (view.getId() == R.id.detail_More) {

        } else if (view.getId() == R.id.image_NewsDetails_Write) {
            ClickUtils.clickComment(this, imageNewsDetailsWrite);

        } else if (view.getId() == R.id.image_NewsDetails_Comment) {

        } else if (view.getId() == R.id.image_NewsDetails_ShouChang) {

            clickShouChang();

        } else if (view.getId() == R.id.image_NewsDetails_Share) {
            ShareDialogUtils.showShareView(this, imageNewsDetailsShare, new ShareDialogUtils.SharePopCallBack() {
                @Override
                public void Call(AdapterView<?> adapterView, View view, int i, long l) {

                }
            });

        }
    }

    private void clickShouChang() {
        if (data == null) {
            LogUtils.startToash(VideoDetailsActivity.this, AppConstant.shouchangnull);
        } else {
            videoId = data.getSubject_id();
            if (videoId == null || videoId.equals("")) {
                LogUtils.startToash(VideoDetailsActivity.this, AppConstant.shouchangnull);
            } else {
                if (isShouChang) {
                    addFavority(3, videoId);

                } else {
                    DelFavority(3, videoId);
                }
            }

        }

    }


    @Override
    protected void onPause() {
        super.onPause();
        videoplayer.releaseAllVideos();
    }


    public void addFavority(int style, String newid) {
        LogUtils.e("添加视频id" + newid + "style " + style);
        UserServiceImpl.AddFavorites(style, newid, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                ResponseBody body = response.body();
                if (body == null) {
                    ResponseBody responseBody = response.errorBody();
                    try {
                        LogUtils.e(responseBody.string());

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        LogUtils.e(body.string());
                        isShouChang = ClickUtils.clickShouChang(VideoDetailsActivity.this, imageShouChang, true);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                LogUtils.e("succ");
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                LogUtils.e("error" + t.getMessage());

            }
        });
    }


    public void DelFavority(int style, String newid) {
        LogUtils.e("删除视频id" + newid);
        UserServiceImpl.DelFavorites(style, newid, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                ResponseBody body = response.body();
                if (body == null) {
                    ResponseBody responseBody = response.errorBody();
                    try {
                        LogUtils.e(responseBody.string());

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        LogUtils.e(body.string());
                        isShouChang = ClickUtils.clickShouChang(VideoDetailsActivity.this, imageShouChang, false);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                LogUtils.e("succ");
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                LogUtils.e("error" + t.getMessage());

            }
        });
    }
}
